<? include_once "../config.php" ;?>

<?


//不需要post数据的时候，直接获取内容，就可以用这个函数
function getWork($url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;
}

?>






<?

session_start();



    echo $sd=date('Y-m-d');
echo $ed=date('Y-m-d',strtotime ("+1 day"));




#file_get_contents对ssl支持问题，所以补充这个，和使用的时候带上参数。
if(!$_SESSION['token']){

//    $stream_opts = [
//        "ssl" => [
//            "verify_peer"=>false,
//            "verify_peer_name"=>false,
//        ]
//    ];

    $token=file_get_contents("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww134f8f020195ca02&corpsecret=EaIBO6-lGEso_41WgSBVtBdZJUK_NUq1_DSNUBUbz0g",
    false,stream_context_create($stream_opts));

    $_SESSION['token']=$token;

echo "更新token完毕：".$token."<br/>";
}

$token_array=json_decode( $_SESSION['token'],true);

//这个就是获取到的token

#print_r2($token_array);

#$token_array['access_token'];

#echo "<br/>";


$newtoken=$token_array['access_token'];
$url = "https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token=".$newtoken."&debug=1";



//那边需要post的参数
//"opencheckindatatype": 3,
//"starttime": 1602518400,
//"endtime": 1602604800,
//"useridlist":["ChenQiQi","WuJunYuan"]



//傍晚的时候获取当天的打卡数据。然后入库。
//之后其他功能api，根据当日的打卡记录，再发送统计数据。




//encode 要他妈的用数组去转换成json
//$starDate = date('2020-10-10');//
//$endDate = date('2020-10-11');//这个是不含当天，要11日的数据需要写成12
//$post['opencheckindatatype']=3;
//$post['starttime']= strtotime($starDate);
//$post['endtime']= strtotime($endDate);
//$post['useridlist']="ChenQiQi";////员工的userid
//
//
//
//
//$data = json_encode($post);
//
//
//$curl = curl_init($url);
//curl_setopt($curl, CURLOPT_HEADER, false);
//curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
//curl_setopt($curl, CURLOPT_POST, true);
//curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
#$content=curl_exec($curl);


#print_r2(json_decode($content,true));

#curl_close($curl);











//首先获取需要的access_token

//$access_token = json_decode($this->getWork($url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww134f8f020195ca02&corpsecret=EaIBO6-lGEso_41WgSBVtBdZJUK_NUq1_DSNUBUbz0g'),true);
//
////access_token获取后将它放置在缓存里，不能频繁调用gettoken接口，否则会受到频率拦截 access_token有效期是2个小时
//Cache::set('access_token',$access_token['access_token'],7200);
//


//获取到access_token之后可以获取当前企业下的部门列表

$department = getWork($url='https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token='.$newtoken);


print_r2($department);


//获取部门下的员工信息userid，循环改id或者直接传多个参数？
$simplelist = json_decode(getWork($url='https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token='.$newtoken.'&department_id=1'),true);

#print_r2($simplelist);


foreach($simplelist['userlist'] as $k=>$v)
{
    #echo $v['userid'],"-".$v['name']."<br/>";

//获取到员工的userid后可以进行当前员工的打卡记录查询
$starDate = $sd;//
$endDate = $ed;//
$url = 'https://qyapi.weixin.qq.com/cgi-bin/checkin/getcheckindata?access_token='.$newtoken;
$post['opencheckindatatype']=3;
$post['starttime']= strtotime($starDate);
$post['endtime']= strtotime($endDate);
#$post['useridlist']=$simplelist['userlist'][7]['userid'];////员工的userid
$post['useridlist']=$v['userid'];////员工的userid





$data = json_encode($post);


$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$content=curl_exec($curl);
//转换得到的结果
$clockin_result=json_decode($content,true);
//    [checkindata] => Array
//(
//    [0] => Array
//           (
//               [userid] => ChenQiQi
//                    [checkin_type] => 上班打卡
//                    [exception_type] => 时间异常
//                    [checkin_time] => 1602291686

//                )

curl_close($curl);

#print_r2($clockin_result);



//转换一下结果，符合以及数组用于传递过函数生成插入语句

    foreach($clockin_result['checkindata'] as $k2=>$v2)
    {
        $check_log[$k2]['userid']         = $v2['userid'];
        $check_log[$k2]['checkin_type']   = $v2['checkin_type'];
        $check_log[$k2]['exception_type'] = $v2['exception_type'];
        $check_log[$k2]['checkin_time']   = date("Y-m-d H:i:s",$v2['checkin_time']);
    }

echo "当前用户的打卡";
print_r2($check_log);


//批量出入多条数据。组装插入数据，一次过插入这个人的所有打卡记录。
$p['table']=$table_Prefix."clock_in";
$p['items']=array("name","checkin_type","exception_type","date");


if(count($check_log)>0)
{
$p['values']=$check_log;

unset($check_log);
echo $sql=insert_datas($p);
echo "<br/>";
$query = mysql_query($sql);
mysql_close($con);
}







}


?>


